<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        img { width:100px; height:100px; position:absolute; top:200px; }
    </style>
    <script>
        function getStyle(obj,attr){return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[attr];}
        function shake(obj,attr,endFn){
            if(obj.shaked){return;}
            obj.shaked = true;
            var pos = parseInt(getStyle(obj,attr));

            var arr = [];// 20, -20, 18, -18 ..... 0
            var num = 0;
            var timer = null;
            for(var i = 20; i > 0; i -=2){
                arr.push(i,-i);
            }
            arr.push(0);
            clearInterval(obj.shake);
            obj.shake = setInterval(function(){
                obj.style[attr] = pos + arr[num] + 'px';
                num++;
                if(num === arr.length){
                    clearInterval(obj.shake);
                    endFn && endFn();
                    obj.shaked = false;
                }
            },100);
        }

        window.onload = function(){
            var imgs = document.getElementsByTagName("img");
            for(var i = 0; i < imgs.length; i++){
                imgs[i].style.left = 80 + i * 120 + 'px';
                imgs[i].onmouseover = function(){
                    shake(this,'top');
                }
            }
        }
    </script>
</head>
<body>
    <img src="./img/1.jpg" alt="">
    <img src="./img/2.jpg" alt="">
    <img src="./img/3.jpg" alt="">
    <img src="./img/4.jpg" alt="">
    <img src="./img/5.jpg" alt="">
    <img src="./img/6.jpg" alt="">
    <img src="./img/7.jpg" alt="">
    <img src="./img/8.jpg" alt="">
</body>
</html>